
//创建类
function goodsList() {
    this.detailData = [];
    this.allData = [];
    this.init();
    this.computed();
}

goodsList.prototype.init = function () {
    let me = this;
    $.getJSON("goodsList.json", "", (data)=> {
        me.allData = data.datas;
        me.deal(me.allData);
    });
    
}
goodsList.prototype.deal = function (domArr){
    let me = this;
    let imgStr = '',domStr = '',classFlag='';
    $.each(domArr, (i,item)=> {
        me.detailData = item.detailData;
        $.each(me.detailData,(index,val)=>{
            index == 0 ? 
            imgStr +=  '<img  src="'+val.url+'" dataId='+item.id+' imgId='+index+' price='+val.price+' class="hover">' : 
            imgStr +=  '<img src="'+val.url+'" dataId='+item.id+' imgId='+index+' price='+val.price+'>'
        })
        
        domStr +=  '<li>'+
                             '<div class="goods-img">'+
                                 '<div class="main-img"><img src="'+me.detailData[0].url+ '" alt=""></div>'+
                                 '<div class="other-imgs">'+
                                     imgStr+
                                 '</div>'+
                             '</div>'+
                             '<div class="goods-desc">'+
                                 '<div class="goods-price"><em>¥</em><i>'+me.detailData[0].price+'.00</i></div>'+
                                 '<a href="" class="goods-title">'+item.title+'</a>'+
                                 '<div class="goods-comments">已有<a href="">'+(item.commentsNum > 10000 ? (parseInt(item.commentsNum/1000)/10 +'万+') : item.commentsNum) +'</a>条评价</div>'+
                             '</div>'+
                         '</li>'
         imgStr = '';            
    })
    $('.goods-list').html(domStr);
}
goodsList.prototype.transComments = function(comment){
  if(comment < 10000){
      return comment
  }else{
      return (comment%1000)/10 + '万+'
  }
}
goodsList.prototype.computed = function () {
    let me = this;
    $('.goods-list').on('mouseover','li .other-imgs img',(ev)=>{
       
        ev.target.parentNode.parentNode.firstChild.firstChild.attributes[0].value = ev.target.attributes[0].value;
        ev.target.parentNode.parentNode.parentNode.lastChild.firstChild.lastChild.innerHTML = ev.target.attributes[3].value+'.00';

        $.each(Array.from(ev.target.parentNode.childNodes),(i,item)=>{
            if(item.className=='hover'){
                item.className = ''
                ev.target.className = 'hover'
            }
        })
    })
    $(".filter-condition ul").on("click",'li',(ev)=>{
        $.each(Array.from(ev.target.parentNode.childNodes),(i,item)=>{
            if(item.className=='hover'){
                item.className = ''
                ev.target.className = 'hover'
            }
        })
        let sortArr = [];
        if(ev.target.innerHTML == '综合排序'){
            sortArr =sortArr = me.allData.sort((a,b)=> a.id - b.id);
            me.deal(sortArr)
        }else if(ev.target.innerHTML == '销量'){
            sortArr = me.allData.sort((a,b)=> a.saledNum - b.saledNum)
            me.deal(sortArr)
        }else if(ev.target.innerHTML == '价格'){
            sortArr = me.allData.sort((a,b)=> a.detailData[0].price - b.detailData[0].price)
            me.deal(sortArr)
        }else if(ev.target.innerHTML == '评论数'){
            sortArr = me.allData.sort((a,b)=> a.commentsNum - b.commentsNum)
            me.deal(sortArr)
        }
    })
}
$(function () {
       new goodsList();
})